package thread.threaddemo;

import java.util.concurrent.TimeUnit;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/20/11:05
 * @Description:死锁示例
 */
public class ThreadDemo18 {
    public static void main(String[] args) {
        Object lockA=new Object();
        Object lockB=new Object();
        Thread t1=new Thread(()->{
           synchronized (lockA){
               System.out.println("线程1获得了A");
               try {
                   TimeUnit.SECONDS.sleep(1);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               synchronized (lockB){
                   System.out.println("线程1或得到B");
               }
           }
        });
        t1.start();

        Thread t2=new Thread(()->{
            synchronized (lockB){
                System.out.println("线程2获得了B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockA){
                    System.out.println("线程2或得到A");
                }
            }
        });
        t2.start();
    }
}
